package cn._51doit.flink.day06;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 给ValueState设置TTL（数据存活时间）
 */
public class ValueStateTTLDemo {


    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //开启checkpoint
        env.enableCheckpointing(10000);

        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String in, Collector<String> out) throws Exception {
                String[] words = in.split(" ");
                for (String word : words) {
                    if (word.startsWith("error")) {
                        throw new RuntimeException("数据出现了问题！！！");
                    }
                    //输出数据
                    out.collect(word);
                }
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = words.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String word) throws Exception {
                return Tuple2.of(word, 1);
            }
        });

        KeyedStream<Tuple2<String, Integer>, String> keyedStream = wordAndOne.keyBy(t -> t.f0);

        //对KeyedStream调用map方法，实现与sum或reduce相同的功能
        SingleOutputStreamOperator<Tuple2<String, Integer>> res = keyedStream.map(new RichMapFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            private ValueState<Integer> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                //配置StateTtlConfig
                StateTtlConfig ttlConfig = StateTtlConfig
                        .newBuilder(Time.seconds(30))
                        //设置状态的更新类型
                        .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite) //默认，在创建或修改该key对应的value时，会重新计时
                        //.setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite) // 在读或写操作，都会重新计时
                        //设置状态的可见性
                        .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired) //默认，超时的就读不到了
                        //.setStateVisibility(StateTtlConfig.StateVisibility.ReturnExpiredIfNotCleanedUp) //只要状态没被清理，即使超时，也可以读取到
                        //设置时间类型
                        .setTtlTimeCharacteristic(StateTtlConfig.TtlTimeCharacteristic.ProcessingTime)
                        .build();
                ValueStateDescriptor<Integer> stateDescriptor = new ValueStateDescriptor<>("count-state", Integer.class);
                //设置stateDescriptor的TTL，即将TTLConfig与stateDescriptor进行关联
                stateDescriptor.enableTimeToLive(ttlConfig);
                //通过运行时上下文初始化或恢复状态
                valueState = getRuntimeContext().getState(stateDescriptor);

            }

            @Override
            public Tuple2<String, Integer> map(Tuple2<String, Integer> tp) throws Exception {
                String word = tp.f0;
                Integer currentCount = tp.f1;
                Integer history = valueState.value();
                if (history == null) {
                    history = 0;
                }
                int sum = history + currentCount;
                //更新状态
                valueState.update(sum);
                tp.f1 = sum;
                //输出数据
                return tp;
            }
        });

        res.print();

        env.execute();

    }

}
